﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PlaceApi;

namespace PersonalClient.Controls
{
    /// <summary>
    /// Interaction logic for AutoCompletionTextBox.xaml
    /// </summary>
    public partial class AutoCompletionTextBox : UserControl
    {
        PlaceAPI api;
        public string CurrentCity { get; set; }
        private Dictionary<string, dynamic> nameJson = new Dictionary<string, object>();
        public static readonly RoutedEvent ResultArrive;
        public event RoutedEventHandler QueryResultArrive
        {

            add { base.AddHandler(ResultArrive, value); }

            remove { base.RemoveHandler(ResultArrive, value); }

        }
        /// <summary>
        /// json object as argument
        /// </summary>
        public event Action<dynamic> SelectOne;

        static AutoCompletionTextBox()
        {
            ResultArrive = EventManager.RegisterRoutedEvent(
    "ResultArrive", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(AutoCompletionTextBox));
        }
        public AutoCompletionTextBox()
        {
            InitializeComponent();
            api = new PlaceAPI("afd2da1b2c4f0b91da0c2d91e37292f6");
        }



        private void riseEvent(object src)
        {
            RaiseEvent(new RoutedEventArgs(ResultArrive, src));
        }
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtSearch.Text.Length == 0)
            {
                pp.IsOpen = false;
            }
        }
        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            //  pp.IsOpen = false;
        }

        private void txtSearch_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                lstResult.Items.Clear();
                nameJson.Clear();
                dynamic info = api.GetResponse(txtSearch.Text, CurrentCity);
                if (info.status == "OK")
                {
                    int c = 0;
                    foreach (dynamic item in info.results)
                    {
                        string key = item.name;
                        if (nameJson.ContainsKey(key) == false)
                        {
                            nameJson.Add(key, item);
                        }
                        lstResult.Items.Add(item.name);
                        c++;
                        if (c > 9)
                        {
                            break;
                        }
                    }
                }
                riseEvent(info);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            QueryResultArrive += new RoutedEventHandler((s, arg) => pp.IsOpen = true);
            App.Current.MainWindow.LocationChanged += new EventHandler(win_LocationChanged);
        }
        void win_LocationChanged(object sender, EventArgs e)
        {
            if (pp.IsOpen)
            {
                pp.IsOpen = false;
                pp.IsOpen = true;
            }
        }

        private void lstResult_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstResult.SelectedValue != null && nameJson.ContainsKey(lstResult.SelectedValue.ToString()))
            {
                object info = nameJson[lstResult.SelectedValue.ToString()];
                if (SelectOne != null)
                {
                    SelectOne(info);
                    pp.IsOpen = false;
                }
            }
        }
    }
}
